package com.invengo.module_check.ui.fragment.firstpos.fragment;

import android.annotation.SuppressLint;
import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableInt;

import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.library_rfid.classify.AnalyzeImpl;
import com.invengo.library_rfid.entity.TagInfo;
import com.invengo.module_check.data.repsitory.business.BusinessRepsitory;

import java8.util.Optional;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.RxUtils;

public class FirstPosViewModel extends BaseViewModel<BusinessRepsitory> {

    public SingleLiveEvent<Integer> index = new SingleLiveEvent<>();
    public SingleLiveEvent<Integer> count = new SingleLiveEvent<>();

    public SingleLiveEvent<String> libName = new SingleLiveEvent<>();
    public SingleLiveEvent<String> shelfName = new SingleLiveEvent<>();
    public SingleLiveEvent<String> shelfCode = new SingleLiveEvent<>();

    public SingleLiveEvent<String> currentBookName = new SingleLiveEvent<>();
    public SingleLiveEvent<String> currentBookBarCode = new SingleLiveEvent<>();
    public SingleLiveEvent<String> currentBookCallNo = new SingleLiveEvent<>();
    public ObservableInt clearBtnVisibility = new ObservableInt();
    ShelfBean shelfBean;
    public SingleLiveEvent<Boolean> showDelete = new SingleLiveEvent<>();
    public SingleLiveEvent<BookBean> bookBean = new SingleLiveEvent<>();
    public SingleLiveEvent<String> newBarCode = new SingleLiveEvent<>();

    public FirstPosViewModel(@NonNull Application application) {
        super(application, BusinessRepsitory.getInstance());
    }

    public UIChangeObservable ui = new UIChangeObservable();

    public class UIChangeObservable {
        public SingleLiveEvent<ShelfBean> removeBookEvent = new SingleLiveEvent<>();
    }

    public BindingCommand removeBook = new BindingCommand(() -> {
        ui.removeBookEvent.postValue(shelfBean);
    });


    public void removeFirstBooks() {
        currentBookName.postValue("暂无");
        currentBookBarCode.postValue("暂无");
        currentBookCallNo.postValue("暂无");
        showDelete.postValue(false);
        Optional.ofNullable(shelfBean).ifPresent(shelfBean -> {
            shelfBean.setFirstBookName("");
            shelfBean.setFirstBookID("");
            shelfBean.setBookIndex("");
        });
    }
    public BindingCommand<String> changeVisiable = new BindingCommand<>(s -> newBarCode.postValue(s));

    public BindingCommand cleanNewBookInfo = new BindingCommand(() -> {
        bookBean.postValue(null);
        Optional.ofNullable(shelfBean).ifPresent(shelfBean -> {
            shelfBean.setFirstBookName("");
            shelfBean.setFirstBookID("");
            shelfBean.setBookIndex("");
        });
    });


    public void initDefaultData(int index, int count, ShelfBean bean) {
        this.index.postValue(index);
        this.count.postValue(count);

        currentBookName.postValue(Optional.ofNullable(bean.getFirstBookName()).orElse("暂无"));
        currentBookBarCode.postValue(Optional.ofNullable(bean.getFirstBookID()).orElse("暂无"));
        currentBookCallNo.postValue(Optional.ofNullable(bean.getBookIndex()).orElse("暂无"));

        shelfCode.postValue(Optional.ofNullable(bean.getShelfCode()).orElse("层架号未知"));
        libName.postValue(Optional.ofNullable(bean.getLibCD()).orElse("书库未知"));
        shelfName.postValue(Optional.ofNullable(bean.getShelfName()).orElse("层架名未知"));
        showDelete.postValue(!TextUtils.isEmpty(bean.getFirstBookName()));
        this.shelfBean = bean;
    }


    public void onScanResult(TagInfo tagInfo) {
        switch (tagInfo.getRfidModel()) {
            case TID:
                if (tagInfo.getScanModel() == AnalyzeImpl.TO_SHELF) {
//                    getShelfByTid(tagInfo.getTag());
                } else
                    getBookByTid(tagInfo.getTag());
                break;
            case EPC:
            case U_DATA:
                if (tagInfo.getScanModel() == AnalyzeImpl.TO_SHELF) {
//                    getShelfByCode(tagInfo.getTag());
                } else
                    getBookByCode(tagInfo.getTag());
                break;
        }

    }

    /**
     * 按Code查询书本
     *
     * @param code 标签
     */
    private void getBookByCode(String code) {
        model.getBookByCode(code)
                .compose(RxUtils.applySchedulersToast(getLifecycleProvider(), this))
                .subscribe(bookBean -> updateNewBook(bookBean));
    }


    /**
     * 按Tid查询书本
     *
     * @param tid 标签
     */
    private void getBookByTid(String tid) {
        model.getBookByTid(tid)
                .compose(RxUtils.applySchedulersToast(getLifecycleProvider(), this))
                .subscribe(bookBean -> updateNewBook(bookBean));
    }


    public void updateNewBook(BookBean bean) {
        bookBean.postValue(bean);
        Optional.ofNullable(shelfBean).ifPresent(shelfBean -> {
            shelfBean.setFirstBookName(bean.getBookName());
            shelfBean.setFirstBookID(bean.getBarCode());
            shelfBean.setBookIndex(bean.getCallNo());
        });
    }

    public BindingCommand<String> textChangeCommand = new BindingCommand<String>(s -> {
        getUC().getHideKeyboardEvent().call();
        getBookByCode(s);
    });
}
